package com.bootdo.act.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bootdo.act.dao.ActTypeDao;
import com.bootdo.act.domain.ActTypeDO;
import com.bootdo.act.service.ActTypeService;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.BuildTree;



@Service
public class ActTypeServiceImpl implements ActTypeService {
	@Autowired
	private ActTypeDao actTypeDao;
	
	@Override
	public ActTypeDO get(Long id){
		return actTypeDao.get(id);
	}
	
	@Override
	public List<ActTypeDO> list(Map<String, Object> map){
		return actTypeDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return actTypeDao.count(map);
	}
	
	@Override
	public int save(ActTypeDO actType){
		return actTypeDao.save(actType);
	}
	
	@Override
	public int update(ActTypeDO actType){
		return actTypeDao.update(actType);
	}
	
	@Override
	public int remove(Long id){
		return actTypeDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return actTypeDao.batchRemove(ids);
	}

	@Override
	public Tree<ActTypeDO> getTree() {
		List<Tree<ActTypeDO>> trees = new ArrayList<Tree<ActTypeDO>>();
		List<ActTypeDO> types = actTypeDao.list(new HashMap<String,Object>(16));
		for (ActTypeDO type : types) {
			Tree<ActTypeDO> tree = new Tree<ActTypeDO>();
			tree.setId(type.getId().toString());
			tree.setParentId(type.getParentId().toString());
			tree.setText(type.getName());
			Map<String, Object> state = new HashMap<>(16);
			state.put("opened", true);
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<ActTypeDO> t = BuildTree.build(trees);
		return t;
	}
	
}
